package com.qqtech.qquan.circle.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleSignDao;
import com.qqtech.qquan.circle.model.CircleSign;

@Service
public class CircleSignServiceImpl extends BaseServiceImpl<CircleSign>
		implements CircleSignService {
	public static final Logger log = LoggerFactory.getLogger(CircleSignServiceImpl.class);
	
	@Resource
	private CircleSignDao circleSignDao;
	@Resource
	private CircleMemberService circleMemberService;
	
	@Override
	protected BaseDao<CircleSign> getBaseDao() {
		return circleSignDao;
	}

	@Override
	public boolean isSignToday(Integer memberId, Integer circleId) {
		String todady = TimeUtil.getStringTimeByFormat(TimeUtil.now(), "yyyyMMdd");//20160907
		CircleSign sign = new CircleSign();
		sign.setCircleId(circleId);
		sign.setMemberId(memberId);
		sign.setSignDate(todady);
		return circleSignDao.queryCount(sign)>0;
	}

	@Override
	public int queryTotalSignCount(Integer memberId, Integer circleId) {
		CircleSign sign = new CircleSign();
		sign.setCircleId(circleId);
		sign.setMemberId(memberId);
		return circleSignDao.queryCount(sign);
	}

	@Override
	public int handleSign(Integer memberId, Integer circleId)  throws BizzException{
		String todady = TimeUtil.getStringTimeByFormat(TimeUtil.now(), "yyyyMMdd");//20160907
		CircleSign addSign = new CircleSign();
		addSign.setCircleId(circleId);
		addSign.setMemberId(memberId);
		addSign.setSignDate(todady);
		int signId = circleSignDao.insert(addSign);
		if(signId>0){
			circleMemberService.increaseSignCount(circleId, memberId);//签到+1
		}
		return signId;
	}
	
}